package io.vov.vitamio.widget;

import io.vov.vitamio.utils.Device;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup.LayoutParams;

/**
 * ���÷�����в��ŵĲ�����
 * 
 * @author ChenRui
 * 
 */
public class ServiceVideoView extends SurfaceView {
	private SurfaceHolder mSurfaceHolder;
	private int mSurfaceWidth, mSurfaceHeight;
	private int mVideoMode = VIDEO_LAYOUT_ORIGIN;
	public static final int VIDEO_LAYOUT_ORIGIN = 0;
	public static final int VIDEO_LAYOUT_SCALE = 1;
	public static final int VIDEO_LAYOUT_STRETCH = 2;
	public static final int VIDEO_LAYOUT_ZOOM = 3;
	public static final int VIDEO_LAYOUT_SCALE_ZOOM = 4;
	public int mVideoHeight;
	private SurfaceHolder.Callback mListener;

	public ServiceVideoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		getHolder().addCallback(mCallback);
		getHolder().setFormat(PixelFormat.RGBA_8888);
	}

	public void initialize(SurfaceHolder.Callback l) {
		mListener = l;
		if (mSurfaceHolder == null)
			mSurfaceHolder = getHolder();
	}

	private void setSurfaceLayout(float userRatio, int videoWidth,
			int videoHeight, float videoAspectRatio) {
		LayoutParams lp = getLayoutParams();
		int windowWidth = Device.getScreenWidth(getContext());
		int windowHeight = Device.getScreenHeight(getContext());
		float windowRatio = windowWidth / (float) windowHeight;
		float videoRatio = userRatio <= 0.01f ? videoAspectRatio : userRatio;
		mSurfaceHeight = videoHeight;
		mSurfaceWidth = videoWidth;
		if (VIDEO_LAYOUT_ORIGIN == mVideoMode && mSurfaceWidth < windowWidth
				&& mSurfaceHeight < windowHeight) {
			lp.width = (int) (mSurfaceHeight * videoRatio);
			lp.height = mSurfaceHeight;
		} else if (mVideoMode == VIDEO_LAYOUT_ZOOM) {
			lp.width = windowRatio > videoRatio ? windowWidth
					: (int) (videoRatio * windowHeight);
			lp.height = windowRatio < videoRatio ? windowHeight
					: (int) (windowWidth / videoRatio);
		} else if (mVideoMode == VIDEO_LAYOUT_SCALE_ZOOM && mVideoHeight > 0) {
			lp.width = (int) (mVideoHeight * videoRatio);
			lp.height = mVideoHeight;
		} else {
			boolean full = mVideoMode == VIDEO_LAYOUT_STRETCH;
			lp.width = (full || windowRatio < videoRatio) ? windowWidth
					: (int) (videoRatio * windowHeight);
			lp.height = (full || windowRatio > videoRatio) ? windowHeight
					: (int) (windowWidth / videoRatio);
		}
		mVideoHeight = lp.height;
		setLayoutParams(lp);
		getHolder().setFixedSize(mSurfaceWidth, mSurfaceHeight);
	}

	public void setVideoLayout(int layout, float userRatio, int videoWidth,
			int videoHeight, float videoRatio) {
		mVideoMode = layout;
//		setSurfaceLayout(userRatio, videoWidth, videoHeight, videoRatio);
	}

	private SurfaceHolder.Callback mCallback = new SurfaceHolder.Callback() {
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			Log.d(VIEW_LOG_TAG, "surfaceChanged");
			holder.setKeepScreenOn(true);
			if (mListener != null)
				mListener.surfaceChanged(holder, format, width, height);
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			Log.d(VIEW_LOG_TAG, "surfaceCreated");
			mSurfaceHolder = holder;
			if (mListener != null)
				mListener.surfaceCreated(holder);
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			Log.d(VIEW_LOG_TAG, "surfaceDestroyed");
			if (mListener != null)
				mListener.surfaceDestroyed(holder);
		}
	};

}
